﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.ToolPositioningInternal.CApTPIObjectManipulator
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.ToolPositioningInternal;

internal class CApTPIObjectManipulator
{
  private TxTransformation _originalObjectPosition;
  private ITxLocatableObject _object;
  private const int _minSlopeDeviation = -17;
  private const int _maxSlopeDeviation = 17;
  private Random _random = new Random();

  public bool IsManipulationInProgress { get; set; }

  public ITxLocatableObject Object
  {
    get => this._object;
    set
    {
      if (value != null)
      {
        if (value == this._object)
          return;
        if (this._object != null && ((ITxObject) this._object).IsValid())
          this._object.AbsoluteLocation = this._originalObjectPosition;
        this._object = value;
        this._originalObjectPosition = this._object.AbsoluteLocation;
        this._object.AbsoluteLocation = new TxTransformation();
        TxTransformation txTransformation = TxTransformation.op_Multiply(this._object.GeometricCenter.Inverse, this._object.AbsoluteLocation);
        ITxLocatableObject itxLocatableObject = this._object;
        itxLocatableObject.AbsoluteLocation = TxTransformation.op_Multiply(itxLocatableObject.AbsoluteLocation, txTransformation);
        this.ObjectLargestSideOrthogonalVector = this.FindLargestBBoxSide(this._object);
      }
      else
      {
        if (this._object == null || !((ITxObject) this._object).IsValid())
          return;
        this._object.AbsoluteLocation = this._originalObjectPosition;
        this._object = (ITxLocatableObject) null;
      }
    }
  }

  public TxVector ObjectLargestSideOrthogonalVector { get; internal set; }

  protected TxVector FindLargestBBoxSide(ITxLocatableObject obj)
  {
    TxTransformation txTransformation = new TxTransformation(obj.AbsoluteLocation);
    TxBoundingBoxCalculator boundingBoxCalculator = new TxBoundingBoxCalculator();
    boundingBoxCalculator.AddObject((ITxObject) obj);
    TxBoundingBox boundingBox = boundingBoxCalculator.CalculateBoundingBox();
    double num1 = (boundingBox.Higher.Z - boundingBox.Lower.Z) * (boundingBox.Higher.X - boundingBox.Lower.X);
    double num2 = (boundingBox.Higher.Y - boundingBox.Lower.Y) * (boundingBox.Higher.X - boundingBox.Lower.X);
    double num3 = (boundingBox.Higher.Y - boundingBox.Lower.Y) * (boundingBox.Higher.Z - boundingBox.Lower.Z);
    return num1 <= num2 || num1 <= num3 ? (num2 <= num3 ? new TxVector(1.0, 0.0, 0.0) : new TxVector(0.0, 0.0, 1.0)) : new TxVector(0.0, 1.0, 0.0);
  }

  private void SetRotationVectorAxisAngle(TxVector vector, AxisType axis, double angleDegrees)
  {
    double num = angleDegrees * (Math.PI / 180.0);
    switch (axis)
    {
      case AxisType.RX:
        vector.X = num;
        break;
      case AxisType.RY:
        vector.Y = num;
        break;
      case AxisType.RZ:
        vector.Z = num;
        break;
    }
  }

  private static void GetOtherAxises(TxVector vec, out AxisType axis2, out AxisType axis3)
  {
    axis2 = AxisType.RY;
    axis3 = AxisType.RZ;
    if (vec.Y == 1.0)
    {
      axis2 = AxisType.RX;
      axis3 = AxisType.RZ;
    }
    else
    {
      if (vec.Z != 1.0)
        return;
      axis2 = AxisType.RX;
      axis3 = AxisType.RY;
    }
  }

  public void Rotate(int numRotations, Action<ITxObject> callBackEachRotation)
  {
    this.IsManipulationInProgress = true;
    TxVector vec = new TxVector(this.ObjectLargestSideOrthogonalVector);
    AxisType axis2;
    AxisType axis3;
    CApTPIObjectManipulator.GetOtherAxises(vec, out axis2, out axis3);
    TxVector txVector = new TxVector();
    while (numRotations-- > 0 && this.IsManipulationInProgress)
    {
      double num = (double) this._random.Next(0, 360) * (Math.PI / 180.0);
      TxVector vector = TxVector.op_Multiply(vec, num);
      int angleDegrees1 = this._random.Next(-17, 17);
      this.SetRotationVectorAxisAngle(vector, axis2, (double) angleDegrees1);
      int angleDegrees2 = this._random.Next(-17, 17);
      this.SetRotationVectorAxisAngle(vector, axis3, (double) angleDegrees2);
      this._object.AbsoluteLocation = TxTransformation.op_Multiply(new TxTransformation(txVector, vector, (TxTransformation.TxRotationType) 1), TxTransformation.op_Multiply(this._object.GeometricCenter.Inverse, this._object.AbsoluteLocation));
      callBackEachRotation((ITxObject) this.Object);
    }
    this.IsManipulationInProgress = false;
  }
}
